package beans;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import domain.Basket;
import dto.Item;


public class DAOCLassBasket implements Serializable {

	private static final long serialVersionUID = 6171158984847946462L;
	private Basket basket = new Basket();
	
	public ArrayList<String> getCatalogue(){
		
		ArrayList<String> catList = new ArrayList<>();
		
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/totalitea", "root", "root");
			Statement stat= con.createStatement();
			ResultSet res= stat.executeQuery("SELECT * FROM product");



			while(res.next())
			{
				catList.add(res.getString("description")+"/"+res.getString("id")+"/"+res.getString("itemWeight")+"/"+res.getString("price"));
			}
			res.close();
			stat.close();
			con.close();
		}
		catch(SQLException exception)
		{
			System.out.println(exception.getMessage());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return catList;
	}
	public Item getProduct(int ID)
	{
		
		List<Item> items = new ArrayList<Item>();
		
		Item newItem = new Item();
		
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/totalitea", "root", "root");
			Statement stat= con.createStatement();
			ResultSet res= stat.executeQuery("SELECT * FROM product WHERE id =" + ID);

			while(res.next())
			{
				System.out.println(res);
				newItem.setId(res.getInt("id"));
				newItem.setDescription(res.getString("description"));
				newItem.setItemWeight(res.getDouble("itemWeight"));
				newItem.setPrice(res.getBigDecimal("price"));
				items.add(newItem);
			}
			res.close();
			stat.close();
			con.close();
			
			basket.setItems(items);
		}
		catch(SQLException exception)
		{
			System.out.println(exception.getMessage());
		} catch (ClassNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newItem;
	}
}
